<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>新增资源</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form layui-form-pane" action="" name="frmMain">
			<div class="mainBox">
				<div class="main-container">
					<div class="layui-form-item ">
						<label class="layui-form-label">名称</label>
						<div class="layui-input-block">
							<input type="text" name="resName" lay-verify="required" autocomplete="off" placeholder="请输入名称"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">类型</label>
						<div class="layui-input-block">
							<select name="res_type" lay-verify="required" lay-filter="res_type">
								<option value="">请选择类型</option>
								<option value="1">目录</option>
								<option value="2">链接</option>
							</select>
						</div>
					</div>
					<div class="layui-form-item" style="display: none">
						<label class="layui-form-label">链接</label>
						<div class="layui-input-block">
							<input type="text" name="link" autocomplete="off" placeholder="请输入链接"
								   class="layui-input">
						</div>
					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="user-save">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
				</div>
			</div>
		</form>
		<script src="../../../config/apiUrl.js"></script>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery','http','popup','common','toast'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let http = layui.http;
				let popup = layui.popup;
				let common = layui.common;
				let toast = layui.toast;
				let res_id = common.getUrlParams("id");

				function closeWin(){
					let layer_index = parent.layer.getFrameIndex(window.name);
					if(layer_index>0){
						parent.layer.close(layer_index); //关闭当前页
					}
				}
				$(document).keyup(function (event) {
					if (event.keyCode == 27) {
						closeWin();
					}
				});
				form.on('select(res_type)', function(data){
					var elem = data.elem; // 获得 select 原始 DOM 对象
					var value = data.value; // 获得被选中的值
					if(value == 1){
						$("[name='link']").parent().parent().hide();
					}else if(value == 2){
						$("[name='link']").parent().parent().show();
					}else{
						$("[name='link']").parent().parent().hide();
					}
				});
				form.on('submit(user-save)', function(data) {
					layer.confirm('确定要保存该资源么', {icon: 3, title: '提示'}, function (index) {
						layer.close(index);
						let loading = layer.load();
						let data_post = data.field;
						data_post.pid = res_id;
						http.ajax({
							url: apiUrlConfig.res.Add,
							dataType: 'json',
							type: 'post',
							data: JSON.stringify(data_post),
							contentType: "application/json",
							success: function (result) {
								layer.close(loading);
								if (result.status == 200) {
									parent.layui.toast.success({title: false,message: '保存成功'})
									const bc = new BroadcastChannel('resListReload');
									bc.postMessage("resListReload");
									closeWin();
								} else {
									if (result.status == 10001) {
										//字段提示
										let errorMsg = JSON.parse(result.msg);
										for (let key in errorMsg) {
											layer.tips(errorMsg[key], $("[name='"+key+"']").parent(), {
												tips: [3,'#f56c6c'],time:5000,tipsMore: true
											});
											console.error(key,errorMsg[key])
										}
									} else {
										toast.error({title: false,message: result.msg})
									}
								}
							},
							error: function (res) {
								layer.close(loading);
							}
						})
					});
					return false;
				});
			})
		</script>
		<script>
		</script>
	</body>
</html>
